<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="700"
    destroy-on-close
    append-to-body
    @closed="emits('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="80px"
      :style="'el-dialog' === 'el-drawer' ? 'padding:0 20px' : ''"
    >

      <el-form-item label="月台" prop="plawms_t_id">
        <el-select v-model="form.plawms_t_id" style="width:100%" clearable placeholder="请选择月台">

          <el-option
            v-for="(item, index) in dictData.wmsBaPlatform"
            :key="index" :label="item.platform_name"
            :value="item.id"
          >{{ item.platform_name }}</el-option>

        </el-select>
      </el-form-item>

      <el-form-item label="车牌号" prop="carno">
        <el-input v-model="form.carno" clearable placeholder="请输入车牌号" />
      </el-form-item>

      <el-form-item label="单据编号" prop="doc_id">
        <el-input v-model="form.doc_id" clearable placeholder="请输入单据编号" />
      </el-form-item>

      <el-form-item label="计划进入时间" prop="plan_indata">
        <el-date-picker
          v-model="form.plan_indata" placeholder="请选择计划进入时间"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="计划驶出时间" prop="plan_outdata">
        <el-date-picker
          v-model="form.plan_outdata" placeholder="请选择计划驶出时间"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="进入时间" prop="in_data">
        <el-date-picker
          v-model="form.in_data" placeholder="请选择进入时间"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="驶出时间" prop="ouwms_t_data">
        <el-date-picker
          v-model="form.ouwms_t_data" placeholder="请选择驶出时间"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="备注" prop="plawms_t_beizhu">
        <el-input v-model="form.plawms_t_beizhu" clearable placeholder="请输入备注" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" :active-value="proxy.$CONFIG.SWITCH.ENABLE" :inactive-value="proxy.$CONFIG.SWITCH.DISABLE" />
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" clearable placeholder="请输入排序" />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
  import { ref, reactive, defineExpose, onMounted,getCurrentInstance } from 'vue'
  import {useRouter} from "vue-router";
  import { ElMessage } from 'element-plus'
  import wmsWmPlawmsTIo from '@/api/apis/wms/wmsWmPlawmsTIo'
	import wmsBaPlatform from '@/api/apis/wms/wmsBaPlatform'
	import systemDict from '@/api/apis/system/dataDict'


  const emits = defineEmits(['success', 'closed'])
  const { proxy } = getCurrentInstance();
  const router = useRouter();

  const mode = ref('add')
  const treeList = ref([])
  const visible = ref(false)
  const isSaveing = ref(false)
  const dialogForm = ref(null)

  const titleMap = reactive({ add: '新增月台', edit: '编辑月台' })
  const dictData = reactive({
    sex: [],
    platform_data_status: [],
		wmsBaPlatform:[],

  })
  const form = reactive({
    id:'',
plawms_t_id:'',
carno:'',
doc_id:'',
plan_indata:null,
plan_outdata:null,
in_data:null,
ouwms_t_data:null,
plawms_t_beizhu:'',
status:proxy.$CONFIG.SWITCH.ENABLE,
sort:'0',
mer_id:'0',

  })
  const rules = reactive({
    plawms_t_id: [{required: true, message: '月台必填', trigger: 'blur' }],
    carno: [{required: true, message: '车牌号必填', trigger: 'blur' }],
    plan_indata: [{required: true, message: '计划进入时间必填', trigger: 'blur' }],
    plan_outdata: [{required: true, message: '计划驶出时间必填', trigger: 'blur' }],

  })

  onMounted(async () => {
    await getDictData()
    if(router.currentRoute.value.query.open) {
    	open('add');
    }
  })

  const getDictData = () => {
    systemDict.getDict('sex').then(res => {
      dictData.sex = res.data
    })
    systemDict.getDict('platform_data_status').then(res => {
      dictData.platform_data_status = res.data
    })

		// 月台列表
		wmsBaPlatform.list().then(res => {
			dictData.wmsBaPlatform = res.data
		})

  }

  const open = (type = 'add') => {
    mode.value = type
    visible.value = true

  }

  const submit = () => {
    dialogForm.value.validate(async (valid) => {
      if (valid) {
        isSaveing.value = true
        const res = mode.value === 'add' ? await wmsWmPlawmsTIo.save(form) : await wmsWmPlawmsTIo.update(form.id, form)
        if (res.success) {
          emits('success', form, mode.value)
          visible.value = false
          ElMessage.success(res.message)
        } else {
          ElMessage.error(res.message)
        }
        isSaveing.value = false
      }
    })
  }

  const setData = (data) => {
    for (let k in form) {
      if (data[k] || data[k] === 0) {
        form[k] = data[k]
      }
    }
  }


  defineExpose({
    open, setData
  })
</script>
